import requests
import numpy as np
import time
import json
import os
import glob

# 全局变量用于跟踪当前读取的文件索引
current_file_index = 0

def generate_random_array():
    global current_file_index
    
    # 获取demo_datas目录下所有的json文件，并按名称排序
    demo_files = sorted(glob.glob('demo_datas/demo*.json'))
    
    if not demo_files:
        raise Exception("No demo files found in demo_datas directory")
    
    # 读取当前索引对应的文件
    with open(demo_files[current_file_index], 'r') as f:
        data = json.load(f)
    
    # 更新索引，实现循环
    current_file_index = (current_file_index + 1) % len(demo_files)
    
    return data

def send_data():
    url = 'http://localhost:5000/submit'
    try:
        data = generate_random_array()
        response = requests.post(url, json=data)
        if response.status_code == 200:
            print(f"发送成功: {data}")
        else:
            print(f"发送失败: {response.status_code} - {response.text}")
    except Exception as e:
        print(f"发送异常: {str(e)}")

if __name__ == '__main__':
    print("开始发送数据，每5秒发送一次...")
    try:
        while True:
            send_data()
            time.sleep(5)
    except KeyboardInterrupt:
        print("\n程序已停止") 